package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.impl;

import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.RelicsHistoricSitesArchitecture;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.RelicsHistoricSitesCurrentAssets;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.RelicsHistoricSitesRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.vo.ResultVo;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/16
 * @Description: 文物古迹地震损失报表
 */
@Repository
public class RelicsHistoricSitesRepositoryImpl implements RelicsHistoricSitesRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String ARCHITECTURE = "xczh_relics_historic_sites_architecture"; // 文物古迹地震损失报表_建（构）筑物部分
    private static final String CURRENT_ASSETS = "xczh_relics_historic_sites_current_assets"; // 文物古迹地震损失报表_流动资产部分

    @Override
    public void deleteByXlrmId(String xlrmId) {
        String sql = "delete from  "+ARCHITECTURE+"  where xlrm_id= '"+xlrmId+"'" ;
        String sql1 = "delete from  "+CURRENT_ASSETS+"  where xlrm_id= '"+xlrmId+"'" ;
        jdbcTemplate.update(sql);
        jdbcTemplate.update(sql1);
    }

    @Override
    public void batchArchitectureSave(List<RelicsHistoricSitesArchitecture> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+ARCHITECTURE +
                " ( id,xlrm_id,fill_form_user,tel,fill_form_time,province,city,county,township,type," +
                "name,construction_time,protection_level,floor_area,structure_type,damage_condition_desc," +
                "destroy_level,loss,recovery_mode,restore_money,sequence,enterprise_name)" +
                "values (:id,:xlrmId,:fillFormUser,:tel,:fillFormTime,:province,:city,:county,:township,:type," +
                ":name,:constructionTime,:protectionLevel,:floorArea,:structureType,:damageConditionDesc," +
                ":destroyLevel,:loss,:recoveryMode,:restoreMoney,:sequence,:enterpriseName)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void batchCurrentSave(List<RelicsHistoricSitesCurrentAssets> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+CURRENT_ASSETS +
                " ( id,xlrm_id,type,name,total_value,damage_condition_desc,loss,restore_money,sequence,province,city,county)" +
                "values (:id,:xlrmId,:type,:name,:totalValue,:damageConditionDesc,:loss,:restoreMoney,:sequence,:province,:city,:county)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<ResultVo> getArchitectureTotalLoss(String xsemId, String province, String city, String county,String reportType) {
        StringBuilder sql = new StringBuilder("select t2.xsem_id,t1.province,t1.city,t1.county, t1.loss " +
                "from xczh_relics_historic_sites_architecture t1 left join xczh_loss_report_manage t2 on t1.xlrm_id=t2.id " +
                "where t2.xsem_id = '"+xsemId+"' and t2.report_type = '"+reportType+"' and t1.type='合计'" +
                " and t1.loss is not null and t1.loss <> ''");
        if (PlatformObjectUtils.isNotEmpty(province)){
            sql.append("and province='"+province+"'");
        }
        if (PlatformObjectUtils.isNotEmpty(city)){
            sql.append("and city='"+city+"'");
        }
        if (PlatformObjectUtils.isNotEmpty(county)){
            sql.append("and county='"+county+"'");
        }
        List<ResultVo> list = namedParameterJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ResultVo.class));
        return list;
    }

    @Override
    public List<ResultVo> getCurrentAssetsTotalLoss(String xsemId, String province, String city, String county,String reportType) {
        StringBuilder sql = new StringBuilder("select t2.xsem_id,t1.province,t1.city,t1.county, t1.loss " +
                "from xczh_relics_historic_sites_current_assets t1 left join xczh_loss_report_manage t2 on t1.xlrm_id=t2.id " +
                "where t2.xsem_id = '"+xsemId+"' and t2.report_type = '"+reportType+"' and t1.type='合计' and t1.loss is not null and t1.loss <> ''");
        if (PlatformObjectUtils.isNotEmpty(province)){
            sql.append("and province='"+province+"'");
        }
        if (PlatformObjectUtils.isNotEmpty(city)){
            sql.append("and city='"+city+"'");
        }
        if (PlatformObjectUtils.isNotEmpty(county)){
            sql.append("and county='"+county+"'");
        }
        List<ResultVo> list = namedParameterJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ResultVo.class));
        return list;
    }
}
